﻿using System;
using System.Windows.Forms;
using Campus.Main;
using Campus.Main.Administrator;
using Campus.Main.Basic;
using Campus.Main.ReadWriteUser;
using Campus.Main.SuperUser;
using Campus.Main.WriteUser;
using Campus.Properties;
using dbcntn;
using wkwxml;

namespace Campus
{
    public partial class Login : Form
    {

        private ConnectionToDb _connectionToDb;
        private BasicUserForm mainform; 
        public Login()
        {
            InitializeComponent();
        }

        void Initialize()
        {
            Helper.ConnectionString = Helper._connectionString; //DataLayer.GetConfiguration();
            MaximizeBox = false;
            MinimizeBox = false;
            _connectionToDb = new ConnectionToDb(Helper.ConnectionString);
            textBox1.Clear();
            textBox2.Clear();
        }

        private void ChildFormClosed(object sender, FormClosedEventArgs e)
        {
            Close();
            Initialize();
        }
        private void ChildFormClosed1(object sender, FormClosedEventArgs e)
        {
            Initialize();
        }
        private void Button1Click(object sender, EventArgs e)
        {
            string userId = textBox1.Text.Trim(), password = textBox2.Text.Trim();
            if (userId == Resources.Login_root && password == Resources.Password_qwerty)
            {
                CreateDb aCreateDb = new CreateDb();
                aCreateDb.FormClosed += ChildFormClosed1;
                aCreateDb.ShowDialog();
            }
            else
            {
                Helper.SId = userId;
                switch (_connectionToDb.Checkuser(userId,password))
                {
                    case "1":
                        mainform=new FormForSuperUser();
                        break;
                    case "2":
                        mainform=new FormForReadUser();
                        break;
                    case "3":
                        mainform=new FormForWriteUser();
                        break;
                    case "4":
                        mainform=new FormForAdministrator();
                        break;
                    default:
                        mainform = null;
                        MessageBox.Show("Берилиштерди туура киргизгиле");
                        DefaultState();
                        break;
                }
                if (mainform != null)
                {
                    mainform.FormClosed += ChildFormClosed;
                    mainform.Show();
                    Hide();
                }
            }
        }
        void DefaultState()
        {
            textBox1.Clear();
            textBox2.Clear();
            textBox1.Focus();
        }
        private void LoginLoad(object sender, EventArgs e)
        {
            Initialize();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar<48||e.KeyChar>57)&&e.KeyChar!=8)
            {
                e.Handled = true;
            }
            else
            {
                e.Handled = false;
            }
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar==(decimal) Keys.Enter)
            {
                Button1Click(null,null);
            }
        }
    }
}
